<?php
/**
 * Created by PhpStorm.
 * User: Lee
 * Date: 2021/5/11
 * Time: 16:04
 */
namespace app\admin\controller;
use app\admin\BaseController;
use app\admin\model\Admin;
use think\captcha\facade\Captcha;
use think\exception\ValidateException;
use think\facade\View;
use think\Request;

class Login extends BaseController

{
    protected function initialize()
    {}
    public function login()
    {
        if(\think\facade\Request::isPost()){
            $post = \think\facade\Request::post();

            try{
                validate(['code'=>'require|captcha'])->check($post);
            }catch (ValidateException $exception){
                $msg = $exception->getMessage();
            }
            if(isset($msg)) $this->error($msg);



            $where = [
                'name' => $post['name'],
                'pwd' => $post['password']
            ];
            $user = Admin::where($where)->find();
            if ($user){
                session('userInfo',$user->toArray());
                $this->success('登陆成功',url('Index/index'));
            }else{
                $this->error('账号或密码错误');
            }
        }
        return view();
    }
    public function verify(){


        return Captcha::create();
    }
}